package com.slipper.system.flow.service;

import java.util.List;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import com.slipper.constants.Constants;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;

import com.slipper.system.flow.dao.FlowSignatureDao;
import com.slipper.system.flow.bo.FlowSignatureBO;

import com.slipper.core.transaction.ReadTransactional;
import com.slipper.core.transaction.WriteTransactional;
import com.slipper.util.Tool;
import com.slipper.util.UUID;

@Service
@ReadTransactional
public class FlowSignatureService {
    @Resource
    private FlowSignatureDao flowSignatureDao;
    
    public FlowSignatureBO queryBy(String id) {
        return flowSignatureDao.queryBy(id);
    }
    
    public List<FlowSignatureBO> queryList(FlowSignatureBO bo) {
        return flowSignatureDao.queryList(bo);
    }
    
    public PageInfo<FlowSignatureBO> queryPage(FlowSignatureBO bo) {
        PageHelper.startPage(bo.getPageNumber(), bo.getPageSize());
        return new PageInfo<>(flowSignatureDao.queryList(bo));
    }
    
    @WriteTransactional
    public int save(FlowSignatureBO bo) {
        if (Tool.isNotBlank(bo.getId())) {
            bo.setUpdateTime(System.currentTimeMillis());
            return flowSignatureDao.update(bo);
        } else {
            FlowSignatureBO update = new FlowSignatureBO();
            update.setEmployeeId(bo.getEmployeeId());
            update.setIfDefault(Constants.NO);
            update.setUpdateId(bo.getUpdateId());
            update.setUpdateTime(System.currentTimeMillis());
            updateDefault(update);

            bo.setId(UUID.getUUID());
            bo.setIfDefault(Constants.YES);
            bo.setUpdateTime(System.currentTimeMillis());
            bo.setCreateTime(System.currentTimeMillis());
            return flowSignatureDao.insert(bo);
        }
    }

    @WriteTransactional
    public void updateDefault(FlowSignatureBO bo) {
        flowSignatureDao.updateDefault(bo);
    }
    
    @WriteTransactional
    public int delete(FlowSignatureBO bo) {
        return flowSignatureDao.delete(bo);
    }

    @WriteTransactional
    public int saveDefault(FlowSignatureBO bo) {
        FlowSignatureBO update = new FlowSignatureBO();
        update.setEmployeeId(bo.getEmployeeId());
        update.setIfDefault(Constants.NO);
        update.setUpdateId(bo.getUpdateId());
        update.setUpdateTime(System.currentTimeMillis());
        updateDefault(update);

        update.setId(bo.getId());
        update.setIfDefault(Constants.YES);
        updateDefault(update);
        return 1;
    }
}
